package com.miage.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.miage.bdd.DaoBdd;
import com.miage.bean.Depense;
import com.miage.bean.Evenement;
import com.miage.bean.Participant;
import com.miage.utils.Constante;

public class DaoDepense extends DaoBdd {
	
	private static final String TABLE = Constante.DEPENSE_TABLE;

	private static final String ATTR_ID = Constante.DEPENSE_COLUMN_ID;
	private static final String ATTR_NOM = Constante.DEPENSE_COLUMN_NOM;
	private static final String ATTR_DESCRIPTION = Constante.DEPENSE_COLUMN_DESCRIPTION;
	private static final String ATTR_MONTANT = Constante.DEPENSE_COLUMN_MONTANT;
	private static final String ATTR_DATE = Constante.DEPENSE_COLUMN_DATE;
	private static final String ATTR_TYPE_DEBIT = Constante.DEPENSE_COLUMN_TYPE_DEBIT;
	private static final String ATTR_TYPE_PAIEMENT = Constante.DEPENSE_COLUMN_TYPE_PAIEMENT;
	private static final String ATTR_ID_PARTICIPANT = Constante.DEPENSE_FOREIGNKEY_ID_PARTICIPANT;
	private static final String ATTR_ID_EVENEMENT = Constante.DEPENSE_FOREIGNKEY_ID_EVENEMENT;

	public DaoDepense(Context pContext) {
		super(pContext);
	}
	
	public void ajouter(Depense d) {
		this.open();
		
		ContentValues value = new ContentValues();
		value.put(ATTR_ID, Integer.valueOf(d.getId()));
		value.put(ATTR_NOM, d.getNom());
		value.put(ATTR_DESCRIPTION, d.getDescription());
		value.put(ATTR_MONTANT, d.getMontant());
		value.put(ATTR_DATE, d.getDate());
		value.put(ATTR_TYPE_DEBIT, d.getTypeDebit());
		value.put(ATTR_TYPE_PAIEMENT, d.getTypePaiement());
		value.put(ATTR_ID_PARTICIPANT, Integer.valueOf(d.getIdParticipant()));
		value.put(ATTR_ID_EVENEMENT, Integer.valueOf(d.getIdEvenement()));

		bdd.insert(TABLE, null, value);
		
		this.close();
	}
	
	public void supprimer(int id) {
		this.open();
		
		bdd.delete(
				TABLE, 
				ATTR_ID + " = ?", 
				new String[] { String.valueOf(id) }
		);
		
		this.close();
	}
	
	public void modifier(Depense d) {
		this.open();
		
		ContentValues value = new ContentValues();
		value.put(ATTR_ID, d.getId());
		value.put(ATTR_NOM, d.getNom());
		value.put(ATTR_DESCRIPTION, d.getDescription());
		value.put(ATTR_MONTANT, d.getMontant());
		value.put(ATTR_DATE, d.getDate());
		value.put(ATTR_TYPE_DEBIT, d.getTypeDebit());
		value.put(ATTR_TYPE_PAIEMENT, d.getTypePaiement());
		value.put(ATTR_ID_PARTICIPANT, d.getTypePaiement());
		value.put(ATTR_ID_EVENEMENT, d.getIdEvenement());
		
		bdd.update(
				TABLE, 
				value, 
				ATTR_ID + " = ?",
				new String[] { String.valueOf(d.getId()) }
			);
		
		this.close();
	}
	
	public Depense getDepenseById(Integer id) {
		this.open();
		
		Cursor c = bdd.query(
						TABLE, new String[] { ATTR_ID, ATTR_NOM, ATTR_DESCRIPTION, ATTR_MONTANT, ATTR_DATE, ATTR_TYPE_DEBIT,
								ATTR_TYPE_PAIEMENT, ATTR_ID_PARTICIPANT, ATTR_ID_EVENEMENT },
						ATTR_ID + " = ?", 
						new String[] { String.valueOf(id) }, 
						null,
						null, 
						null, 
						null
					);
		
		if (c != null) {
			c.moveToFirst();
		}
		
		this.close();
		
		return new Depense(
				c.getInt(c.getColumnIndex(ATTR_ID)), 
				c.getString(c.getColumnIndex(ATTR_NOM)),
				c.getString(c.getColumnIndex(ATTR_DESCRIPTION)),
				c.getFloat(c.getColumnIndex(ATTR_MONTANT)),
				c.getString(c.getColumnIndex(ATTR_DATE)),
				c.getString(c.getColumnIndex(ATTR_TYPE_DEBIT)),
				c.getString(c.getColumnIndex(ATTR_TYPE_PAIEMENT)),
				c.getInt(c.getColumnIndex(ATTR_ID_PARTICIPANT)),
				c.getInt(c.getColumnIndex(ATTR_ID_EVENEMENT))
		);
	}
	
	public List<Depense> getDepensesByEvenement(Evenement e) {
		this.open();
		
		List<Depense> listDepense = new ArrayList<Depense>();
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_NOM, ATTR_DESCRIPTION, ATTR_MONTANT, ATTR_DATE, ATTR_TYPE_DEBIT,
						ATTR_TYPE_PAIEMENT, ATTR_ID_PARTICIPANT, ATTR_ID_EVENEMENT },
						ATTR_ID_EVENEMENT + " = ?", 
				new String[] { String.valueOf(e.getId()) },  
				null,
				null, 
				null, 
				null
			);
		
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			listDepense.add(
					new Depense(
							cursor.getInt(cursor.getColumnIndex(ATTR_ID)), 
							cursor.getString(cursor.getColumnIndex(ATTR_NOM)),
							cursor.getString(cursor.getColumnIndex(ATTR_DESCRIPTION)),
							cursor.getFloat(cursor.getColumnIndex(ATTR_MONTANT)),
							cursor.getString(cursor.getColumnIndex(ATTR_DATE)),
							cursor.getString(cursor.getColumnIndex(ATTR_TYPE_DEBIT)),
							cursor.getString(cursor.getColumnIndex(ATTR_TYPE_PAIEMENT)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_PARTICIPANT)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_EVENEMENT))
					)
			);
		}
		cursor.close();
		this.close();
		
		return listDepense;
	}
	public List<Depense> getDepenses() {
		this.open();
		
		List<Depense> listDepense = new ArrayList<Depense>();
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_NOM, ATTR_DESCRIPTION, ATTR_MONTANT, ATTR_DATE, ATTR_TYPE_DEBIT,
						ATTR_TYPE_PAIEMENT, ATTR_ID_PARTICIPANT, ATTR_ID_EVENEMENT },
				null, 
				null, 
				null,
				null, 
				null, 
				null
			);
		
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			listDepense.add(
					new Depense(
							cursor.getInt(cursor.getColumnIndex(ATTR_ID)), 
							cursor.getString(cursor.getColumnIndex(ATTR_NOM)),
							cursor.getString(cursor.getColumnIndex(ATTR_DESCRIPTION)),
							cursor.getFloat(cursor.getColumnIndex(ATTR_MONTANT)),
							cursor.getString(cursor.getColumnIndex(ATTR_DATE)),
							cursor.getString(cursor.getColumnIndex(ATTR_TYPE_DEBIT)),
							cursor.getString(cursor.getColumnIndex(ATTR_TYPE_PAIEMENT)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_PARTICIPANT)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_EVENEMENT))
					)
			);
		}
		cursor.close();
		this.close();
		
		return listDepense;
	}
	
	public List<Depense>  getDepensesByParticipant(Participant p) {
		this.open();
		List<Depense> listDepense = new ArrayList<Depense>();
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_NOM, ATTR_DESCRIPTION, ATTR_MONTANT, ATTR_DATE, ATTR_TYPE_DEBIT,
						ATTR_TYPE_PAIEMENT, ATTR_ID_PARTICIPANT, ATTR_ID_EVENEMENT },
				ATTR_ID_PARTICIPANT + " = ?", 
				new String[] { String.valueOf(p.getId()) }, 
				null,
				null, 
				null, 
				null
		);
		
	
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			listDepense.add(
					new Depense(
							cursor.getInt(cursor.getColumnIndex(ATTR_ID)), 
							cursor.getString(cursor.getColumnIndex(ATTR_NOM)),
							cursor.getString(cursor.getColumnIndex(ATTR_DESCRIPTION)),
							cursor.getFloat(cursor.getColumnIndex(ATTR_MONTANT)),
							cursor.getString(cursor.getColumnIndex(ATTR_DATE)),
							cursor.getString(cursor.getColumnIndex(ATTR_TYPE_DEBIT)),
							cursor.getString(cursor.getColumnIndex(ATTR_TYPE_PAIEMENT)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_PARTICIPANT)),
							cursor.getInt(cursor.getColumnIndex(ATTR_ID_EVENEMENT))
					)
			);
		}
		cursor.close();
		this.close();
		return listDepense;
		
	}
	
	public float getMontantTotalByParticipant(Participant p) {
		this.open();
		
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_NOM, ATTR_DESCRIPTION, ATTR_MONTANT, ATTR_DATE, ATTR_TYPE_DEBIT,
						ATTR_TYPE_PAIEMENT, ATTR_ID_PARTICIPANT, ATTR_ID_EVENEMENT },
				ATTR_ID_PARTICIPANT + " = ?", 
				new String[] { String.valueOf(p.getId()) }, 
				null,
				null, 
				null, 
				null
		);
		
		float sum = 0.0f;
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			sum += cursor.getFloat(cursor.getColumnIndex(ATTR_MONTANT));
		}
		cursor.close();
		this.close();
		
		return sum;
	}
	
	
	
	/* VERSION 2 ?? 

	public Evenements getEvenements() {
		Evenements events = new Evenements();
		
		List<Evenement> listEvenements = new ArrayList<Evenement>();
		Cursor cursor = bdd.query(
				TABLE, 
				new String[] { ATTR_ID, ATTR_LIBELLE },
				null, 
				null, 
				null,
				null, 
				null, 
				null
			);
		
		for (cursor.moveToFirst() ; !cursor.isAfterLast() ; cursor.moveToNext()) {
			listEvenements.add(
					new Evenement(
							cursor.getLong(cursor.getColumnIndex(ATTR_ID)),
							cursor.getString(cursor.getColumnIndex(ATTR_LIBELLE))
					)
			);
		}
		cursor.close();
		events.setListeEvt(listEvenements);
		return events;
	}
	
	*/

}
